var t = getApp(),
    a = t.requirejs("core"),
    e = t.requirejs("jquery");
Page({
  data: {
    mapId: "myMap",
    longitude: "",
    latitude: "",
    markers: [],
    lat: "",
    lng: "",
    name: "",
    address: "",
    merchid: 0,
    cover: false,
    selected: "",
    selectcover: false,
    placeholder: "输入关键字搜索",
    search: "",
    province: "",
    provid: "",
    city: "",
    cityid: "",
    stype: "",
  },
  onLoad: function (p) {
    this.setData({
      type: p.type,
    })
    // var that = this;
    // wx.getLocation({
    //   type: "wgs84",
    //   success: function (res) {
    //     var latitude = res.latitude;
    //     var longitude = res.longitude;
    //     that.setData({
    //       latitude: latitude,
    //       longitude: longitude,
    //     })
    //   }
    // })
  },
  onShow: function() {
    var that = this, search = that.data.search, province = that.data.province, city = that.data.city, stype = that.data.stype, place = "";
    if(stype){
      if(search && !province && !city){
        place = search;
      }else if(search && province && city){
        place = search+" 区域："+province;
      }else if(province || (province && city)){
        place = "区域："+province;
      }else{
        // place = "门店名称/区域搜索";
        place = "门店名称";
      }
      that.setData({
        placeholder: place
      })
    }
    that.setData({
      imgUrl: t.globalData.approot,
    }),
    wx.getLocation({
      type: "gcj02",
      success: function(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude,
        }), that.getList()
      },
      fail: function(t) {
          a.alert("取消位置信息将无法定位商家距离！"), that.setData({
            latitude: "",
            longitude: "",
          }), that.getList()
      }
    })
  },
  getList: function() {
    var that = this;
    a.get("changce/merch/get_list", {
      lat: that.data.longitude,
      lng: that.data.latitude,
      keyword: that.data.search,
      provinceid: that.data.provid,
      cityid: that.data.cityid
    }, function(e) {
      let list = e.list;
      // 循环添加可识别的经纬度参数
      for (var i = 0; i < list.length; i++) {
        list[i].latitude = list[i].lng,
        list[i].longitude = list[i].lat;
      }
      // console.log(e.list);
      e.list = list;
      that.setData({
        markers: e.list,
        show: !0
      })
    })
  },
  // 地图放大缩小事件触发
  bindregionchange: function(e) {
    // console.log('=bindregiοnchange=', e)
  },
  // 点击标识点触发
  /*markertap: function(e){
    console.log(e);
    var th = this, i = 0,
        lat = "", lng = "", tel = "", time = "", name = "", address = "",
        markerId = e.detail.markerId,
        list = th.data.markers;
    for(i=0; i<list.length; i++){
      if(list[i].id == markerId){
        tel = list[i].tel,
        lat = parseFloat(list[i].latitude),
        lng = parseFloat(list[i].longitude),
        name = list[i].merchname,
        address = list[i].address;
      }
    }
    th.setData({
      tel: tel,
      time: time,
      lat: lat,
      lng: lng,
      name: name,
      address: address,
      cover: true,
    })
  },*/
  // 点击地图事件，有经纬度信息返回
  bindtapMap: function(e) {
    // console.log('=bindtapMap=', e);
  },
  navigation: function(e){
    var that = this;
    var lat = parseFloat(e.currentTarget.dataset.lat), 
        lng = parseFloat(e.currentTarget.dataset.lng), 
        nme = e.currentTarget.dataset.name,  
        add = e.currentTarget.dataset.address;
    wx.openLocation({
      latitude: lat,
      longitude: lng,
      name: nme,
      address: add
    })
    that.setData({ cover: false })
  },
  close: function(){
    this.setData({ cover: false })
  },
  bindInput: function() {
    wx.navigateTo({
      url: '/store/map/search',
    })
  },
  moveTolocation: function () {
    let Id = this.data.mapId
    var mapCtx = wx.createMapContext(Id);
    mapCtx.moveToLocation();
  },
  // select: function(event) {
  //   var that = this, id = event.currentTarget.dataset.id,
  //       lat = event.currentTarget.dataset.lat, lng = event.currentTarget.dataset.lng,
  //       name = event.currentTarget.dataset.name, address = event.currentTarget.dataset.address;
  //   that.setData({
  //     lat: lat, lng: lng,
  //     name: name,
  //     address: address,
  //     selectcover: true,
  //     selected: id,
  //     merchid: id,
  //   })
  // },
  goToHome: function() {
    let type = this.data.type, merchid = this.data.merchid;
    wx.navigateTo({
      url: "/store/index/index?type="+type+"&merchid="+merchid,
    })
  },
  call: function(p) {
    let phone = p.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: phone,
      success:function() { console.log("成功！"); },
      fail:function() { console.log("失败！"); }
    })
  },
  loca: function(p) {
    let lat = parseFloat(p.currentTarget.dataset.lat), lng = parseFloat(p.currentTarget.dataset.lng), 
        nme = p.currentTarget.dataset.name, add = p.currentTarget.dataset.address;
    wx.openLocation({
      latitude: lat,
      longitude: lng,
      name: nme,
      address: add
    })
  },
  url: function(p) {
    var url = url = p.currentTarget.dataset.url;
    wx.navigateTo({ url: url, })
  },
})
